package org.kjtc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kjtc.entity.Manufacturer;
import org.kjtc.entity.Page;
import org.kjtc.service.ActionHistoryService;
import org.kjtc.service.ManufacturerService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ManufacturerInfoController extends BaseController{

	@Resource(name = "actionHistoryService")
	private ActionHistoryService actionHistoryService;

	@Resource(name = "manufacturerService")
	private ManufacturerService manufacturerService;
	@RequestMapping(value = "/searchManufacturerInfo")
	@ResponseBody
	public Map<String, Object> searchManufacturerInfo(Manufacturer manufacturer, String page, String rows){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "厂家信息查询开始");
		// 分页
		Page pageQuery = getPage();
		pageQuery.setCurrentPage(Integer.parseInt(page));
		pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
		pageQuery.setShowCount(Integer.parseInt(rows));
		List<Manufacturer> list = null;
		try {
			list = manufacturerService.getManufacturerPage(manufacturer, pageQuery);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "厂家信息查询异常:" + e.getMessage());
			return map;
		}
		map.put("totalCnt",  pageQuery.getTotalResult());
		map.put("totalPage", pageQuery.getTotalPage());
		map.put("manufacturerList", list);
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "厂家信息查询结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "厂家信息查询");
		manufacturer = null;
		return map;
    }

	/**
	 * 厂家信息添加
	 */
	@ResponseBody
	@RequestMapping(value="/saveManufacturerInfo")
	public Map<String, Object> saveManufacturerInfo(Manufacturer manufacturer){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "厂家信息添加开始");
		manufacturer.setCreateUser(this.getUesr().getUserID());
		manufacturer.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = manufacturerService.saveManufacturer(manufacturer);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (DuplicateKeyException ex) {
			String[] error = ex.getCause().getMessage().split("'");
			if ("ManufacturerID".equals(error[3].trim())) {
				map.put("result", "ManufacturerIDNoNull");
			}
			if("PRIMARY".equals(error[3].trim())) {
				map.put("result", "ManufacturerIDNoNull");
			}
			if("ManufacturerName".equals(error[3].trim())) {
				map.put("result", "ManufacturerNameNoNull");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "厂家信息添加异常:" + e.getMessage());
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "厂家信息添加结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "厂家信息添加");
		manufacturer = null;
		return map;
	}
	/**
	 * 厂家信息修改
	 * @return
	 */
	@RequestMapping(value = "/updateManufacturerInfo")
	@ResponseBody
	public Object updateManufacturerInfo(Manufacturer manufacturer){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "厂家信息修改开始");
		manufacturer.setCreateUser(this.getUesr().getUserID());
		manufacturer.setUpdateUser(this.getUesr().getUserID());
		int count = 0;
		try {
			count = manufacturerService.updateManufacturer(manufacturer);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (DuplicateKeyException ex) {
			String[] error = ex.getCause().getMessage().split("'");
			if ("ManufacturerID".equals(error[3].trim())) {
				map.put("result", "ManufacturerIDNoNull");
			}
			if("PRIMARY".equals(error[3].trim())) {
				map.put("result", "ManufacturerIDNoNull");
			}
			if("ManufacturerName".equals(error[3].trim())) {
				map.put("result", "ManufacturerNameNoNull");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "厂家信息修改异常:" + e.getMessage());
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "厂家信息修改结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "厂家信息修改");
		manufacturer = null;
		return map;
	}
	/**
	 * 删除一条或多条厂家信息
	 */
	@RequestMapping(value = "/deleteManufacturerInfo")
	@ResponseBody
	public Object deleteManufacturerInfo(String manufacturerIDList){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "厂家信息删除开始");
		String[] manufacturerID = manufacturerIDList.split(",");
		Manufacturer manufacturer = new Manufacturer();
		manufacturer.setManufacturerIDList(manufacturerID);
		int count = 0;
		try {
			count = manufacturerService.deleteManufacturer(manufacturer);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "厂家信息删除异常:" + e.getMessage());
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "厂家信息删除结束");
		actionHistoryService.saveActionHistory(this.getUesr().getUserID(), "厂家信息删除");
		manufacturer = null;
		return map;
	}
}
